﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using MixScriptWeb.Share;
using MixScript.BLL;
using MixScriptWeb.control;
using System.Web.UI.WebControls;
using MixScript.BLL.FI;
using System.Data;

namespace MixScriptWeb.Accounting
{
    public partial class fPosting : cCommonWeb, IMIxCommon //
    {
        public static DataTable dt;
        private static SortDirection sd;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) { LoadData(); }

        }

        private void LoadData()
        {
            int isPosting;
            int.TryParse(Request.QueryString["isPosting"], out isPosting);
            if (isPosting == 0)
            {
                lblTitle.Text = "Posting Journal";
                btnProces.Text = "Posting";
            }
            else
            {
                lblTitle.Text = "Open Posted Journal";
                btnProces.Text = "Open Posting";
            }

            string datenow;
            datenow = string.Format("{0:dd-MM-yyyy}", DateTime.Now.ToShortDateString());
            txtFrom.Text = datenow;
            txtTo.Text = datenow;
            int typeJournal = 0;
            int.TryParse(ddlJournalType.SelectedItem.Value.ToString(), out typeJournal);

            DataTable dt = new DataTable();
            Posting pst = new Posting();
            pst.getPosting(out dt, isPosting, typeJournal, DateTime.Parse(txtFrom.Text), DateTime.Parse(txtTo.Text));
            gvList.DataSource = dt;
            gvList.DataBind();
        }
        public bool isvalid()
        {
            return true;
        }
        protected void btnRefresh_click(object sender, EventArgs e)
        {
            int isPosting;
            int.TryParse(Request.QueryString["isPosting"], out isPosting);
            int typeJournal = 0;
            int.TryParse(ddlJournalType.SelectedItem.Value.ToString(), out typeJournal);

            dt = new DataTable();
            Posting pst = new Posting();
            if (pst.getPosting(out dt, isPosting, typeJournal, DateTime.Parse(txtFrom.Text), DateTime.Parse(txtTo.Text)))
            {


            }
            else
            {
                mn = (MainMenu)this.Master;
                ShowStatusBar("No Data Found On Current period");
            }
            bindGridview(gvList, dt);

        }
        protected void btnProces_click(Object sender, EventArgs e)
        {
            int isPosting;
            int.TryParse(Request.QueryString["isPosting"], out isPosting);
            Posting pst = new Posting();
            for (int i = 0; i < gvList.Rows.Count - 1; i++)
            {
                CheckBox chk = new CheckBox();
                chk = (CheckBox)gvList.Rows[i].FindControl("chkAll");
                if (chk.Checked)
                {
                    int id;
                    int.TryParse(gvList.Rows[i].Cells[1].Text.ToString(), out id);
                    if (isPosting == 0)
                        pst.ExecutePosting(id, true);
                    else
                        pst.ExecutePosting(id, false);

                }

                
            }
            btnRefresh_click(null, null);

        }


        protected void gvList_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (sd == SortDirection.Ascending)
            {
                sd = SortDirection.Descending;
            }
            else
            {
                sd = SortDirection.Ascending;
            }

            DataView dataView = new DataView(dt);
            dataView.Sort = e.SortExpression + " " + ShortDirection(sd);

            bindGridview(gvList, dataView);
        }
        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            gvList.PageIndex = e.NewPageIndex;
            bindGridview(gvList, dt);

        }
        protected void chkAllHeader_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkAll = new CheckBox();
            CheckBox chk;
            chkAll = (CheckBox)gvList.HeaderRow.Cells[0].FindControl("chkAllHeader");
            for (int i = 0; i < gvList.Rows.Count; i++)
            {
                chk = new CheckBox();
                chk = (CheckBox)gvList.Rows[i].Cells[0].FindControl("chkAll");
                chk.Checked = chkAll.Checked;

            }

        }

    }
}